#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "dma_task.h"

extern BOOL volatile Dma_isDone;
extern uint8_t ch;
  

#define TRANSFER_SIZE 						128

uint32_t src_arry[TRANSFER_SIZE];
uint32_t dst_arry[TRANSFER_SIZE];


int DMA_Mem_to_Mem()  
{
	int i;
	HME_DMAC_InitTypeDef dmainit;
	BOOL flag;
	Dma_isDone = FALSE;
	ch = 0;

	NVIC_EnableIRQ(DMAC_IRQn);

	HME_DMA_CFGREG(DMA,true);
	
	for(ch=0;ch<DMA_TOTAL_CHANNEL_CNT;ch++)
	{
		
		for(i=0;i<TRANSFER_SIZE;i++)
		{
			src_arry[i] = i;
			dst_arry[i] = 0;
		}
		
		//dma mem to mem cfg
		dmainit.transtype 		= Dmac_mem2mem_dma;
		dmainit.SRC_ADDR 		= (uint32_t *)src_arry;
		dmainit.TAR_ADDR 		= (uint32_t *)dst_arry;
		dmainit.SINC 			= Dmac_addr_increment;
		dmainit.DINC 			= Dmac_addr_increment;
		dmainit.BLOCK_TS 		= TRANSFER_SIZE;
		dmainit.SRC_TR_WIDTH 	= Dmac_trans_width_32;
		dmainit.DST_TR_WIDTH 	= Dmac_trans_width_32;
		dmainit.SRC_MSIZE 		= Dmac_msize_4;
		dmainit.DEST_MSIZE 		= Dmac_msize_4;
			
		flag = HME_DMA_Init(ch,(HME_DMAC_InitTypeDef *)&dmainit);
		if(!flag)
			while(1){};

		while(!Dma_isDone){};   						//wait dma transfer complete	
		Dma_isDone = FALSE;
		
		DMA_ITConfig(ch, DMA_Int_All, FALSE);
		HME_DMA_DISABLE_CH_INTR(DMA,ch);
		HME_DMA_CHENREG_DISABLE(ch);
		
		if(memcmp(src_arry,dst_arry,TRANSFER_SIZE) != 0)
		{	
			printf("ch[%d] error\n", ch);
			while(1){};
		}
	
		printf("DMA channel[%d] data verification is OK\n", ch);
	}
	
	NVIC_DisableIRQ(DMAC_IRQn);
	
	return 1 ;
}
